﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 无线打分网站.webAPI;
using WFNetLib;

namespace 无线打分网站.Manage
{
    public partial class ExperimentEdit : System.Web.UI.Page
    {
        ExperimentData ed;
        string strid;
        protected void Page_Load(object sender, EventArgs e)
        {
            strid = Request.QueryString["id"];            
            if (!IsPostBack)
            {
                if (strid != null && strid != "")//修改
                {
                    Guid id = new Guid(strid);
                    ed = ExperimentDataDBOption.GetExperimentData(id);
                    TextBox1.Text = ed.Name;
                    TextBox2.Text = ed.Room;
                    JSON_ExperimentTime[] times = JsonSerializeHelper.DeSerialize<JSON_ExperimentTime[]>(ed.Times);
                    if (times.Length != 0)
                    {
                        for (int j = 0; j < times.Length; j++)
                        {
                            ListItem li = new ListItem();
                            li.Text = times[j].StartTime.ToString().Insert(2, ":") + "～" + times[j].EndTime.ToString().Insert(2, ":");
                            li.Value = times[j].StartTime.ToString();
                            li.Attributes.Add("s", times[j].StartTime.ToString());
                            li.Attributes.Add("e", times[j].EndTime.ToString());
                            ListBox1.Items.Add(li);                            
                        }                        
                    }
                    if (ed.Week1)
                        CheckBox1.Checked = true;
                    else
                        CheckBox1.Checked = false;
                    if (ed.Week2)
                        CheckBox2.Checked = true;
                    else
                        CheckBox2.Checked = false;
                    if (ed.Week3)
                        CheckBox3.Checked = true;
                    else
                        CheckBox3.Checked = false;
                    if (ed.Week4)
                        CheckBox4.Checked = true;
                    else
                        CheckBox4.Checked = false;
                    if (ed.Week5)
                        CheckBox5.Checked = true;
                    else
                        CheckBox5.Checked = false;
                    if (ed.Week6)
                        CheckBox6.Checked = true;
                    else
                        CheckBox6.Checked = false;
                    if (ed.Week7)
                        CheckBox7.Checked = true;
                    else
                        CheckBox7.Checked = false;
                }
                else
                {
                    ed = null;
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Remove(ListBox1.SelectedItem);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int t1=int.Parse(TextBox3.Text);
            ListItem li = new ListItem();
            li.Text = TextBox3.Text.Insert(2, ":") + "～" + TextBox4.Text.Insert(2, ":");
            li.Value = TextBox3.Text;
            li.Attributes.Add("s", TextBox3.Text);
            li.Attributes.Add("e", TextBox4.Text);
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                int t = int.Parse(ListBox1.Items[i].Value);
                if (t1 < t)
                {
                    ListBox1.Items.Insert(i,li);
                    return;
                }
            }
            ListBox1.Items.Add(li);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            ed = new ExperimentData();
            ed.Name = TextBox1.Text;
            ed.Room = TextBox2.Text;
            JSON_ExperimentTime[] times = new JSON_ExperimentTime[ListBox1.Items.Count];
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                times[i] = new JSON_ExperimentTime();
                times[i].StartTime = int.Parse(ListBox1.Items[i].Value);
                times[i].EndTime = int.Parse(ListBox1.Items[i].Text.Substring(6, 2) + ListBox1.Items[i].Text.Substring(9, 2));
            }
            ed.Times = JsonSerializeHelper.Serialize(times);
            if (CheckBox1.Checked)
                ed.Week1 = true;
            else
                ed.Week1 = false;
            if (CheckBox2.Checked)
                ed.Week2 = true;
            else
                ed.Week2 = false;
            if (CheckBox3.Checked)
                ed.Week3 = true;
            else
                ed.Week3 = false;
            if (CheckBox4.Checked)
                ed.Week4 = true;
            else
                ed.Week4 = false;
            if (CheckBox5.Checked)
                ed.Week5 = true;
            else
                ed.Week5 = false;
            if (CheckBox6.Checked)
                ed.Week6 = true;
            else
                ed.Week6 = false;
            if (CheckBox7.Checked)
                ed.Week7 = true;
            else
                ed.Week7 = false;            
            if (strid != null && strid != "")//修改
            {                
                ed.ID=new Guid(strid);
                ExperimentDataDBOption.Update(ed);
                //Response.Write("<script language=javascript>alert('修改成功');if (window.opener != undefined){ window.opener.returnValue ='" + str + "'; window.close();}else{window.returnValue = '" + str + "';}window.close();</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('修改成功');GetDataAndClose();</script>");//
            }
            else
            {
                ExperimentDataDBOption.Insert(ed);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加成功');GetDataAndClose(); </script>");
            }
        }
    }
}